def merge(nums,l,mid,r):
    if l>=r: return 0
    lst=[]
    i,j=l,mid+1
    k=0
    while i<=mid and j<=r:
        if nums[i]<=nums[j]:
            lst.append(nums[i])
            i+=1
        else:
            lst.append(nums[j])
            j+=1
            k+=mid-i+1 #**
    if i<=mid:lst+=nums[i:mid+1]
    elif j<=r: lst+=nums[j:r+1]
    #print(l,r,mid,lst)
    for i in range(l,r+1):nums[i]=lst[i-l]
    return k 

def core(nums,l,r):
    if l>=r: return 0
    mid=(l+r)//2
    return core(nums,l,mid)+core(nums,mid+1,r)+merge(nums,l,mid,r)

def solve(nums):
    return core(nums,0,len(nums)-1)

nums=[7,5,6,4]
print(solve(nums))


